//
//  PointOfSell.m
//  auditorias
//
//  Created by Mauro on 7/21/13.
//  Copyright (c) 2013 Antilope. All rights reserved.
//

#import "PointOfSell.h"
#import "DataLoader.h"
#import "Comment.h"

@implementation PointOfSell

@synthesize internalId;
@synthesize externalId;
@synthesize name;
@synthesize description;
@synthesize clientCode;
@synthesize address;
@synthesize comments;
@synthesize statusType;
@synthesize geoLocation;


- (void) dealloc
{
    [internalId release], internalId = nil;
    [externalId release], externalId = nil;
    [name release], name = nil;
    [description release], description = nil;
    [clientCode release], clientCode = nil;
    [address release], address = nil;
    [comments release], comments = nil;
    [super dealloc];
}


- (id) initWithDictionary:(NSDictionary*)dictionary
{
    self = [super init];
    self.externalId = (![DataLoader isNull:[dictionary objectForKey:@"id"]]) ? [dictionary objectForKey:@"id"] : @"";
    self.internalId = (![DataLoader isNull:[dictionary objectForKey:@"internalId"]]) ? [dictionary objectForKey:@"internalId"] : @"";
    self.name = (![DataLoader isNull:[dictionary objectForKey:@"name"]]) ? [dictionary objectForKey:@"name"] : @"";
    self.clientCode = (![DataLoader isNull:[dictionary objectForKey:@"clientCode"]]) ? [dictionary objectForKey:@"clientCode"] : @"";
    self.address = (![DataLoader isNull:[dictionary objectForKey:@"address"]]) ? [dictionary objectForKey:@"address"] : @"";
    NSString* addressDesc = [self.address isEqualToString:@""]? @"" : [NSString stringWithFormat:@" - %@",self.address ];
    NSString* clientCodeDesc = [self.clientCode isEqualToString:@""]? @"" : [NSString stringWithFormat:@" - %@",self.clientCode ];
    self.description = [NSString stringWithFormat:@"%@%@%@",self.name,addressDesc,clientCodeDesc ];
    
    NSDictionary* geoLocationObject = (![DataLoader isNull:[dictionary objectForKey:@"geoLocation"]])? [dictionary objectForKey:@"geoLocation"] : nil;
    geoLocation.latitude  = (![DataLoader isNull:[geoLocationObject objectForKey:@"latitude"]])? [[geoLocationObject objectForKey:@"latitude"] doubleValue] : 0;
    geoLocation.longitude  = (![DataLoader isNull:[geoLocationObject objectForKey:@"longitude"]])? [[geoLocationObject objectForKey:@"longitude"] doubleValue] : 0;
    
    self.statusType = ( [DataLoader isNull:[dictionary objectForKey:@"state"]] ? PDV_TYPE_NONE :
                       ([[dictionary objectForKey:@"state"] caseInsensitiveCompare:@"REJECTED"]==NSOrderedSame? PDV_TYPE_REJECTED :
                        ([[dictionary objectForKey:@"state"] caseInsensitiveCompare:@"PENDING"]==NSOrderedSame? PDV_TYPE_PENDING : PDV_TYPE_APPROVED))
                       );
    
    if (![DataLoader isNull:[dictionary objectForKey:@"comments"]]) {
        NSMutableArray* commentsArrAux= [[NSMutableArray alloc] init ];
        NSArray* commentsDicArray = [dictionary objectForKey:@"comments"];
        for (NSDictionary* commentDic in commentsDicArray) {
            Comment *comment = [[Comment alloc] initWithDictionary:commentDic];
            if (comment!=nil && (![comment.message isEqualToString:@""]|| ![comment.evaluatorMessage isEqualToString:@""]))
                [commentsArrAux addObject:comment];
            [comment release];
        }
        self.comments = [[[NSArray alloc] initWithArray:commentsArrAux] autorelease];
        [commentsArrAux release];
    }
    else{
        self.comments = [[[NSArray alloc] init] autorelease];
    }
    
    return self;
}

- (id) initWithLocation:(CLLocationCoordinate2D)_geoLocation description:(NSString*)_description
{
    self = [super init];
    self.externalId = @"";
    self.internalId = [NSString stringWithFormat:@"%.0f",([[NSDate date] timeIntervalSince1970]*1000) ];
    self.geoLocation = _geoLocation;
    self.clientCode = @"0000";
    self.name = _description;
    self.address = @"";
    self.description = _description;
    self.statusType = PDV_TYPE_PENDING;
    self.comments = [[[NSArray alloc] init] autorelease];
    return self;
}


- (NSString*) toInternalJSON
{
    return [NSString stringWithFormat:@"["
            "\n%@"
            "\n]",[self toJSON]];
}

- (NSString*) toJSON
{
    NSString* pdvId = [self.externalId isEqualToString:@""]? @"" : [NSString stringWithFormat:@"         \"id\":\"%@\",",self.externalId];
    return 	[NSString stringWithFormat:@"      {"
             "\n%@"
             "\n         \"internalId\":\"%@\","
             "\n         \"name\":\"%@\","
             "\n         \"address\":\"%@\","
             "\n         \"geoLocation\":{"
             "\n            \"latitude\":%f,"
             "\n            \"longitude\":%f"
             "\n         }"
             "\n      }",pdvId,self.internalId,self.name,self.address,self.geoLocation.latitude,self.geoLocation.longitude];
    
}

+ (NSString*) getIdFromPath:(NSString*) pathPDV
{
    NSRange range = [pathPDV rangeOfString:PDV_FILE_PREFIX];
    NSString *substring = [pathPDV substringFromIndex:NSMaxRange(range)] ;
    return substring;
}


- (NSString*) getFileName
{
    return [NSString stringWithFormat:@"%@%@",PDV_FILE_PREFIX,self.internalId];
}

@end
